<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
*/

class cloudattach_api{

	var $version="0.0.1";
	var $console=true;
	var $registeredEvents = array('afterpostshare','deleteAttach','aftereditshare','cropimagestart','cropimageend');

	public function __construct() {
		$this->pz = &get_instance();
		$settingapi = load_api('setting');
		$this->setting = $settingapi->getSetting('cloudattach');
	}

	public function afterinstall(){
		$settingapi = load_api('setting');
		$setting = $settingapi->getSetting('cloudattach');


		$settingapi->updateSetting('cloudattach',$setting);
		return true;
	}

	public function afteruninstall(){
		return true;
	}

	//*************************Event functions start***********************************

	//$args: source, provider
	public function cropimagestart($args){

		list($source, $provider) = $args;

		//if($this->setting['open']&&$provider){
		if($provider){
			$providerapi = load_api('cloudattach/'.$provider);
			if($providerapi){
				$path = $this->createPathsFromAbsolute($source);
				$providerapi->download($path);
			}
		}
	}

	//$args: source, dest, provider
	public function cropimageend($args){
		list($source,$dest, $provider) = $args;
		if($this->setting['open']&&$provider){
			$providerapi = load_api('cloudattach/'.$provider);
			if($providerapi){
				$path = $this->createPathsFromAbsolute($dest);
				$result = $providerapi->upload($path);
				if($result['success']){
					@file_exists($source)&&@unlink($source);
					@file_exists($dest)&&@unlink($dest);
				}
			}
		}
	}


	public function afterpostshare($agrs){
		$this->processupload($agrs);
	}

	public function deleteAttach($args){
		$attach = $args[0];
		if($this->setting['open']&&$attach['cloud']){
			$provider = $attach['cloud'];
			$providerapi = load_api('cloudattach/'.$provider);

			if($providerapi){
				$data = $this->createDatas($attach);
				foreach ($data as $file){
					$result = $providerapi->delete($file);
				}
			}
		}
	}

	public function aftereditshare($args){
		$share = $args[0];
		if($this->setting['open']&&$share['cloud']){
			$provider = $share['cloud'];
			$providerapi = load_api('cloudattach/'.$provider);
			if($providerapi){
				$attachapi = load_api('attachment');
				if($attaches = $attachapi->findDBAttachByItem($share['item_id'])){
					foreach ($attaches as $attach){
						if(!$attach['cloud']){
							if($this->uploadattach($attach, $providerapi)){
								$this->markattach($attach, $provider);
								if(!$this->setting['backup']){
									$attachapi->deleteAttachFiles($attach);
								}
							}
						}
					}
				}
			}
		}
	}


	//*************************Event functions end***********************************

	private function createPathsFromAbsolute($absolute){
		$dir = dirname($absolute);
		(!is_dir($dir))&&@mkdir($dir,0777,true);
		return array('absolute'=>$absolute,'relative'=>str_replace(BASEPATH, '', $absolute),'dir'=>$dir);
	}

	private function createDatas($attach){
		if($attach['isimage']){
			return array(
					array('absolute'=>BASEPATH.$attach['attachment'].'_large.'.$attach['fileext'],'relative'=>$attach['attachment'].'_large.'.$attach['fileext']),
					array('absolute'=>BASEPATH.$attach['attachment'].'_middle.'.$attach['fileext'],'relative'=>$attach['attachment'].'_middle.'.$attach['fileext']),
					array('absolute'=>BASEPATH.$attach['attachment'].'_small.'.$attach['fileext'],'relative'=>$attach['attachment'].'_small.'.$attach['fileext']),
					array('absolute'=>BASEPATH.$attach['attachment'].'_square_like.'.$attach['fileext'],'relative'=>$attach['attachment'].'_square_like.'.$attach['fileext']),
					array('absolute'=>BASEPATH.$attach['attachment'].'_square.'.$attach['fileext'],'relative'=>$attach['attachment'].'_square.'.$attach['fileext']),
					//array('absolute'=>BASEPATH.$attach['attachment'].'_home.'.$attach['fileext'],'relative'=>$attach['attachment'].'_home.'.$attach['fileext'])
			);
		}else{
			return array(
					array('absolute'=>BASEPATH.$attach['attachment'].'.'.$attach['fileext'],'relative'=>$attach['attachment'].'.'.$attach['fileext'])
			);
		}
	}


	private function processupload($agrs){
		if($this->setting['open']){
			$provider = $this->setting['provider'];
			$providerapi = load_api('cloudattach/'.$provider);
			list($uid, $title, $itemid) = $agrs;
			$attachapi = load_api('attachment');
			$shareapi = load_api('share');
			if($attaches = $attachapi->findDBAttachByItem($itemid)){
				foreach ($attaches as $attach){
					if(!$this->uploadattach($attach, $providerapi))
						return;
				}
				$this->markitem($itemid, $provider);
				if(!$this->setting['backup']){
					foreach ($attaches as $attach){
						$this->markattach($attach, $provider);
						$attachapi->deleteAttachFiles($attach);
					}
				}else{
					foreach ($attaches as $attach){
						$this->markattach($attach, $provider);
					}
				}
			}
		}
	}

	private function markitem($itemid,$provider){
		return load_api('share')->updateDBItem($itemid,array('cloud'=>$provider));
	}


	private function markattach($attach,$provider){
		return load_api('attachment')->updateDBAttach($attach['aid'],$attach['item_id'],array('cloud'=>$provider));
	}

	private function uploadattach($attach,$providerapi){
		if($attach){
			$data = $this->createDatas($attach);
			$uploadrs = true;
			foreach ($data as $file){
				$result = $providerapi->upload($file);
				if(!$result['success']){
					$uploadrs = false; // if one file is faild, the whole share is not cloud type
					return $uploadrs;
				}
			}
			return $uploadrs;
		}
		return false;
	}
}